//
//  RecordDetailViewController.m
//  IphoneMapSdkDemo
//
//  Created by huck on 2017/6/6.
//  Copyright © 2017年 Baidu. All rights reserved.
//

#import "RecordDetailViewController.h"
#import "AppUtils.h"
#import "HomeManager.h"
#import "MJPhoto.h"
#import "MJPhotoBrowser.h"

@interface RecordDetailViewController ()

@property (nonatomic,strong) WaterRecodeItemData *data;
@property (nonatomic,strong) NSMutableArray *imageViewList; //固定+可变 [巡查图片] [整治图片]

@end

@implementation RecordDetailViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initSubView];
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)initSubView
{
    self.title = _waterName;
    _imageViewList = [[NSMutableArray alloc] init];
    _imageViewList = [[NSMutableArray alloc] init];
    [_imageViewList addObject:[NSMutableArray array]];//巡查图片
    [_imageViewList addObject:[NSMutableArray array]];//整治图片
    [HomeManager sharedInstance].waterRecordDetail = nil;
    [self initData];
}

//初始化
-(void)initData
{
    if(![AppUtils hasConnected:YES]){
        return;
    }
    [Toast loading];
    [[HomeManager sharedInstance] requestWaterRecodeDetail:_recordId complete:^(BOOL success) {
        [Toast loaded];
        [self updateInfo];
    }];
}


//整体刷新界面
-(void)updateInfo
{
    _data= [HomeManager sharedInstance].waterRecordDetail;
    _nameLabel.text = _data.waterName;
    _recordDayLabel.text = _data.scoutTime;
    _personLabel.text = _data.scoutName;
    _reresultTypeLabel.text =  InspectResultTypeOK== _data.resultType ? @"合格" :@"不合格" ;
    NSString *superviseText = _data.supervisePropose;
    CGFloat superViewH = [AppUtils labelSizeWithString:superviseText font:[UIFont systemFontOfSize:14] maxSize:CGSizeMake(SCREEN_WIDTH-112, SCREEN_HEIGHT)].height;
    _superLableH.constant = MAX(superViewH, 17);
    _superviseLabel.text = superviseText;
    
    [self updateBadReasonView];
    
    NSString *manOption = [AppUtils isEmpty:_data.manageOpinion] ? @"无": _data.manageOpinion;
    CGFloat manOptionH = [AppUtils labelSizeWithString:manOption font:[UIFont systemFontOfSize:14] maxSize:CGSizeMake(SCREEN_WIDTH-112, SCREEN_HEIGHT)].height;
    _statusLabelH.constant = MAX(manOptionH, 18);
    _statusLabel.text = manOption;
    _statusView.hidden = NO;
    
    [_imageViewList[0] removeAllObjects];
    [_imageViewList[1] removeAllObjects];
    //图片
    for(NSDictionary *picDict in _data.inspectingUrls){
        UIImageView *imageView = [[UIImageView alloc] init];
        [AppUtils setImage:imageView url:picDict[@"picUrl"]];
        imageView.accessibilityValue = picDict[@"picId"];
        [_imageViewList[0] addObject:imageView];
    }
    for(NSDictionary *picDict in _data.manageFinishUrls){
        UIImageView *imageView = [[UIImageView alloc] init];
        [AppUtils setImage:imageView url:picDict[@"picUrl"]];
        imageView.accessibilityValue = picDict[@"picId"];
        [_imageViewList[1] addObject:imageView];
    }
    
    _addPicView.hidden = [AppUtils isEmptyArray:_imageViewList[0]];
    _dealPicView.hidden = [AppUtils isEmptyArray:_imageViewList[1]];
    [self initImageViews:0];
    [self initImageViews:1];
}

//刷新一点一策的界面
-(void)updateBadReasonView
{
    if(_data.showYDYC){
        
        _badReasonView.hidden = NO;
        _badTitleH.constant = 40;
        _badReasonViewH.constant = 40 +5;
        
        [AppUtils clearSubView:_reasonContainner];
        CGFloat offy = 0;
        
        for(int i=0 ;i<_data.badReasonAnalysis.count;i++){
            BadReasonItemData *item = _data.badReasonAnalysis[i];
            UIView *addView  = [AppUtils addReasonView:CGRectMake(0, offy, _reasonContainner.width, 20) title:i==0?@"原因分析:":nil reasonLabel:[[HomeManager sharedInstance] getReanalysisLabel:item.value] resonDetail:item.detail showDetail:item.showDetail target:self selector:@selector(showDetailBtnClick:)];
            if(addView != nil){
                [_reasonContainner addSubview:addView];
                offy += addView.height;
                addView.tag = i;
            }
            
        }
        for(int i=0 ;i<_data.badManageScheme.count;i++){
            BadReasonItemData *item = _data.badManageScheme[i];
            UIView *addView  = [AppUtils addReasonView:CGRectMake(0, offy, _reasonContainner.width, 20) title:i==0?@"整改方案:":nil reasonLabel:[[HomeManager sharedInstance] getMaschemeLabel:item.value] resonDetail:item.detail showDetail:item.showDetail target:self selector:@selector(showDetailBtnClick:)];
            if(addView != nil){
                [_reasonContainner addSubview:addView];
                offy += addView.height;
                addView.tag = i+100;
            }
        }
        _badReasonViewH.constant +=offy;
    }else{
        _badReasonView.hidden = YES;
        _badReasonViewH.constant = 0;
        _badTitleH.constant = 0;
    }
}

-(void)showDetailBtnClick:(UIGestureRecognizer *) recognizer
{
    NSInteger tag = [recognizer.view superview].tag;
    if(tag>=100 && tag-100<_data.badManageScheme.count){
        BadReasonItemData *item = _data.badManageScheme[tag-100];
        item.showDetail = !item.showDetail;
        
    }else if(tag>=0 && tag<_data.badReasonAnalysis.count){
        BadReasonItemData *item = _data.badReasonAnalysis[tag];
        item.showDetail = !item.showDetail;
    }
    [self updateBadReasonView];
}

//刷新照片界面 上下左右顶死 //图片比例3:2, 外面套view,间隔10 index 0-巡查图片 1-整治
- (void)initImageViews:(NSInteger)index
{
    NSMutableArray *resultImageViewList = _imageViewList[index];
    UIView *containerView = index==0 ? _addPicContainerView : _dealPicContainerView;
    NSLayoutConstraint *containerViewH = index==0 ? _addPicViewH : _dealPicViewH;
    [AppUtils clearSubView:containerView];
    if([AppUtils isEmptyArray:resultImageViewList]){
        containerViewH.constant = 0;
        return;
    }
    
    UIImageView *imageView = nil;//图片比例3:2
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat stepX = 10;//左右间隔10
    CGFloat StepY = 10;//上下间隔10
    CGFloat kViewWidth = (SCREEN_WIDTH - 20*2-stepX)/2;
    CGFloat kViewHeight = kViewWidth/1.5 ;
    for (int i = 0; i < resultImageViewList.count; i++) {
        x = (i % 2) * (kViewWidth+stepX);
        y = (i / 2) * (kViewHeight+StepY);
        UIView *outView = [[UIView alloc] initWithFrame:CGRectMake(x, y, kViewWidth, kViewHeight)];
        outView.tag =index;//判断图片是那部分的
        imageView = resultImageViewList[i];
        imageView.frame = outView.bounds;
        imageView.tag = i;//判断查看大图的索引
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.layer.masksToBounds = YES;
        imageView.layer.cornerRadius = 3;
        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewImage:)]];
        [outView addSubview:imageView];
        [containerView addSubview:outView];
    }
    containerViewH.constant = y + kViewHeight +35+5;
}


//查看缩略图
- (void)viewImage:(UITapGestureRecognizer *)recognizer
{
    
    NSInteger part = recognizer.view.superview.tag;
    NSMutableArray *photos = [[NSMutableArray alloc] init];
    MJPhoto *photo = nil;
    UIImageView *imageView = nil;
    NSMutableArray *list = _imageViewList[part];
    for (int i = 0; i < list.count; i++) {
        imageView = [list objectAtIndex:i];
        photo = [[MJPhoto alloc] init];
        photo.image = imageView.image;
        photo.srcImageView = imageView;
        [photos addObject:photo];
    }
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
    browser.photos = photos;
    browser.currentPhotoIndex = recognizer.view.tag;
    [browser show];
}


@end
